{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

<div class="job-status-panel boxed-section active-deployment" data-test-job-status-panel>
  <div class="boxed-section-head hds-foreground-primary">
    <div class="boxed-section-row"
      {{did-insert (action this.establishOldAllocBlockIDs)}}
      >
        <h2>Status:
          <Hds::Badge @text="Deploying {{@job.latestDeployment.shortId}}" @color="highlight" @type="filled" />
        </h2>
        <div class="pull-right">
          {{#if @job.latestDeployment.isRunning}}
            <Hds::Button
              data-test-fail
              {{on "click" (perform this.fail)}}
              disabled={{this.fail.isRunning}}
              @color="critical"
              @text="Fail Deployment"
              {{keyboard-shortcut
                label="Fail Deployment"
                pattern=(array "f" "a" "i" "l")
                action=(perform this.fail)
              }}
            />
          {{/if}}
        </div>
      </div>
    </div>
    <div class="boxed-section-body {{if @job.latestDeployment.requiresPromotion "requires-promotion"}}">
      {{#if @job.latestDeployment.requiresPromotion}}
        <div class="canary-promotion-alert">
          {{#if this.canariesHealthy}}
            <Hds::Alert @type="inline" @color="warning" as |A|>
              <A.Title>Deployment requires promotion</A.Title>
              <A.Description>Your deployment requires manual promotion — all canary allocations have passed their health checks.</A.Description>
              <A.Button
                {{keyboard-shortcut
                  pattern=(array "p" "r" "o" "m" "o" "t" "e")
                  action=(action (perform this.promote))
                }}
                data-test-promote-canary @text="Promote Canary" @color="primary" {{on "click" (perform this.promote)}} />
            </Hds::Alert>
          {{else}}
            {{#if this.someCanariesHaveFailed}}
              <Hds::Alert @type="inline" @color="critical" as |A|>
                <A.Title>Some Canaries have failed</A.Title>
                <A.Description>Your canary allocations have failed their health checks. Please have a look at the error logs and task events for the allocations in question.</A.Description>
              </Hds::Alert>
            {{else}}
              <Hds::Alert @type="inline" @color="neutral" as |A|>
                <A.Title>Checking Canary health</A.Title>
                {{#if this.deploymentIsAutoPromoted}}
                  <A.Description>Your canary allocations are being placed and health-checked. If they pass, they will be automatically promoted and your deployment will continue.</A.Description>
                {{else}}
                  <A.Description>Your job requires manual promotion, and your canary allocations are being placed and health-checked.</A.Description>
                {{/if}}
              </Hds::Alert>
            {{/if}}
          {{/if}}
        </div>
      {{/if}}

      <div class="deployment-allocations">
        {{#if this.oldVersionAllocBlockIDs.length}}
          <h4 class="title is-5 previous-allocations-heading" data-test-old-allocation-tally>
            <span>
              Previous allocations: {{#if this.oldVersionAllocBlocks.running}}{{this.oldRunningHealthyAllocBlocks.length}} running{{/if}}
            </span>

            <section class="versions">
              <ul>
                {{#each this.oldVersions as |versionObj|}}
                  <li>
                    <LinkTo data-version={{versionObj.version}} @route="jobs.job.allocations" @model={{@job}} @query={{hash version=(concat '[' versionObj.version ']')    status=(concat '["running", "pending", "failed"]')         }}>
                      {{#if (eq versionObj.version "unknown")}}
                        <Hds::Badge @text="unknown" class="version-label" @type="inverted" />
                      {{else}}
                        <Hds::Badge @text={{concat "v" versionObj.version}} class="version-label" @type="inverted" />
                      {{/if}}
                      <Hds::Badge @text={{versionObj.allocations.length}} class="version-count" />
                    </LinkTo>
                  </li>
                {{/each}}
              </ul>
            </section>
          </h4>
          <div class="previous-allocations">
            <JobStatus::AllocationStatusRow @allocBlocks={{this.oldVersionAllocBlocks}} @steady={{true}} />
          </div>
          <div class="legend-and-summary" data-test-previous-allocations-legend>
            <legend>
              <span class="legend-item {{if (eq (get this.oldRunningHealthyAllocBlocks "length") 0) "faded"}}">
                <span class="represented-allocation running"></span>
                <span class="count">{{get this.oldRunningHealthyAllocBlocks "length"}} Running</span>
              </span>
              <span class="legend-item {{if (eq (get this.oldCompleteHealthyAllocBlocks "length") 0) "faded"}}">
                <span class="represented-allocation complete"></span>
                <span class="count">{{get this.oldCompleteHealthyAllocBlocks "length"}} Complete</span>
              </span>
            </legend>
          </div>

        {{/if}}

        <h4 class="title is-5" data-test-new-allocation-tally><span>New allocations: {{this.newRunningHealthyAllocBlocks.length}}/{{this.totalAllocs}} running and healthy</span>
          <span class="versions">
          <LinkTo data-version={{@job.version}} @route="jobs.job.allocations" @model={{@job}} @query={{hash version=(concat '[' @job.version ']')}}>
            <Hds::Badge @text={{concat "v" @job.version}} @type="inverted" class="version-label" />
          </LinkTo>
          </span>
        </h4>
        <div class="new-allocations">
          <JobStatus::AllocationStatusRow @allocBlocks={{this.newVersionAllocBlocks}} />
        </div>
      </div>

      <div class="legend-and-summary" data-test-new-allocations-legend>

        {{!-- Legend by Status, then by Health, then by Canary --}}
        <legend>
          {{#each-in this.newAllocsByStatus as |status count|}}
            <ConditionalLinkTo
              @condition={{and (not (eq status "unplaced")) (gt count 0)}}
              @route="jobs.job.allocations"
              @model={{@job}}
              @query={{hash status=(concat '["' status '"]') version=(concat '[' this.job.latestDeployment.versionNumber ']')}}
              @class="legend-item {{if (eq count 0) "faded"}}"
              @label="View {{status}} allocations"
            >
              <span class="represented-allocation {{status}}"></span>
              <span class="count">{{count}} {{capitalize status}}</span>
            </ConditionalLinkTo>
          {{/each-in}}

          {{#each-in this.newAllocsByHealth as |health count|}}
            <span class="legend-item {{if (eq count 0) "faded"}}">
              <span class="represented-allocation legend-example {{health}}">
                <span class="alloc-health-indicator">
                  {{#if (eq health "healthy")}}
                    <Hds::Icon @name="check" @color="#25ba81" @isInline={{true}} />
                  {{else if (eq health "unhealthy")}}
                    <Hds::Icon @name="x" @color="#c84034" @isInline={{true}} />
                  {{else}}
                    <Hds::Icon @name="running" @color="black" class="not-animated" @isInline={{true}} />
                  {{/if}}
                </span>
              </span>
              <span class="count">{{count}} {{humanize health}}</span>
            </span>
          {{/each-in}}

          <span class="legend-item {{if (eq this.newAllocsByCanary.canary 0) "faded"}}">
            <span class="represented-allocation legend-example canary">
              <span class="alloc-canary-indicator" />
            </span>
            <span class="count">{{this.newAllocsByCanary.canary}} Canary</span>
          </span>

        </legend>

        <JobStatus::FailedOrLost
          @rescheduledAllocs={{this.rescheduledAllocs}}
          @restartedAllocs={{this.restartedAllocs}}
          @job={{@job}}
          @supportsRescheduling={{true}}
        />

      </div>

      <div class="history-and-params">
        <JobStatus::DeploymentHistory @deployment={{@job.latestDeployment}} />
        <JobStatus::UpdateParams @job={{@job}} />
      </div>

    </div>
</div>
